package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class TestController {
    private static final String _REDIS_KEY="myapplication_test";

    //Spring Boot 自动装配机制
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/setval")
    public void setVal(String val){
        redisTemplate.opsForValue().set(_REDIS_KEY,val,1000, TimeUnit.SECONDS);  //得到操作redis的类型
    }

    @RequestMapping("/getval")
    public String getValue(){
        return (String)redisTemplate.opsForValue().get(_REDIS_KEY);
    }
}
